package com.jk.libsm.service.impl;

import com.jk.libsm.mapper.UserMappper;
import com.jk.libsm.pojo.User;
import com.jk.libsm.service.UserService;
import com.jk.libsm.utils.DateUtil;
import com.jk.libsm.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lk
 * @date 2020/7/6 0006 15:06
 * @description:
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMappper userMappper;

    @Override
    public Result userLogin(String RNo, String ReaderPassword) {
        String password = userMappper.getUserPasswordById(RNo);
        Result result;
        if (password == null) {
            result = new Result("400", "用户不存在", "");
        } else if (!password.equals(ReaderPassword)) {
            result = new Result("400", "用户名或者密码错误", "");
        } else {
            String code = "200";
            String msg = "登陆成功";
            HashMap<String, String> data = new HashMap<>();
            data.put("token",RNo);
            result = new Result(code,msg,data);
        }
        return result;

    }

    @Override
    public Result userRegister(User user) {
        user.setReaderPermission(1);//新注册用户设置为许可权限
        Result result;
        try{
            userMappper.saveReader(user);
            result = new Result("200","处理成功","");
        }catch (Exception e){
            e.printStackTrace();
            result = new Result("400","处理失败","");
        }
        return result;
    }

    @Override
    public Result userFindBorrowHistory(String token) {
        Result result;
        ArrayList<Bookhistory> list;
        try{
            list =userMappper.getBoorowHistory(token);
            ArrayList<Map<String,Object>> list1=new ArrayList<>();
            for(Bookhistory bookhistory:list){
                HashMap<String,Object> map=new HashMap<>();
                map.put("book_id",bookhistory.getBNo());
                map.put("book_name",bookhistory.getBName());
                map.put("book_author",bookhistory.getBauthor());
                map.put("book_type",bookhistory.getBKName());
                map.put("book_press",bookhistory.getBpublic());
                map.put("position",bookhistory.getPosition());
                map.put("borrow_date",bookhistory.getBorrowdate());
                map.put("return_date",bookhistory.getReturndate());
                list1.add(map);
            }
            return result = new Result("200", "处理成功", list1);
        }catch (Exception e){
            e.printStackTrace();
            return result = new Result("400", "处理失败", "");
        }
    }

    @Override
    public Result userFindBorrowState(String token) {
        Result result;
        try {
            ArrayList<BorrowBook> list = userMappper.getBorrowBook(token);
            ArrayList<Map<String, Object>> list1 = new ArrayList<>();
            for (BorrowBook borrowBook : list) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("book_id",borrowBook.getBNo());
                map.put("book_name",borrowBook.getBName());
                map.put("book_author",borrowBook.getBauthor());
                map.put("book_type",borrowBook.getBKName());
                map.put("book_press",borrowBook.getBpublic());
                map.put("position",borrowBook.getPosition());
                map.put("borrow_date",borrowBook.getBorrowdate());
                map.put("should_return_date",borrowBook.getShouldreturndate());
                map.put("renew_state",borrowBook.getReborrow_state());
                list1.add(map);
            }
            return result = new Result("200", "处理成功", list1);

        }catch (Exception e){
            e.printStackTrace();
            return result = new Result("400", "处理失败", "");
        }
    }

    @Override
    public Result userDelay(String token, int book_id) {
        Result result;
        try{
            String shouldreturndate = userMappper.getShouldreturndate(token, book_id);
            shouldreturndate = DateUtil.plusDay(15,shouldreturndate);
            userMappper.setshouldreturndate(token,book_id,shouldreturndate);
            result = new Result("200", "处理成功", "");
        }catch (Exception e){
            e.printStackTrace();
            result = new Result("400", "处理失败", "");
        }
        return result;
    }

    @Override
    public Result userOrder(String token, int book_id) {
        Result result;
        try{
            String date=DateUtil.getNowDate();
            userMappper.insertAppDate(token,book_id,date);
            userMappper.updateBookIsAppinment(book_id);
            result = new Result("200", "处理成功", "");
        }catch (Exception e){
            e.printStackTrace();
            result = new Result("400", "处理失败", "");

        }
        return result;
    }

    @Override
    public Result userFindOrderState(String id) {
        Result result;
        try{
            ArrayList<Bookhistory> list;
            list =userMappper.getAppDate(id);
            ArrayList<Map<String,Object>> list1=new ArrayList<>();
            for(Bookhistory bookhistory:list){
                HashMap<String,Object> map=new HashMap<>();
                map.put("book_id",bookhistory.getBNo());
                map.put("book_name",bookhistory.getBName());
                map.put("book_author",bookhistory.getBauthor());
                map.put("book_type",bookhistory.getBKName());
                map.put("book_press",bookhistory.getBpublic());
                map.put("position",bookhistory.getPosition());
                map.put("order_date",bookhistory.getAppDate());
                list1.add(map);
            }
            result = new Result("200", "处理成功", list1);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            result = new Result("400", "处理失败", "");
            return result;
        }
    }

    @Override
    public Result userGetUserInfo(String token) {
        ArrayList<Userinfo> list;
        list =userMappper.getUserInfo(token);
        ArrayList<Map<String,Object>> list1=new ArrayList<>();
        for(Userinfo userinfo:list){
            HashMap<String,Object> map=new HashMap<>();
            map.put("id",userinfo.getRNo());
            map.put("name",userinfo.getRName());
            map.put("phone_num",userinfo.getReaderPhonenumber());
            map.put("gender",userinfo.getRSex());
            map.put("address",userinfo.getReaderAddress());
            list1.add(map);
        }
        Result result = new Result("200", "处理成功", list1);
        return result;
    }
}
